/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.testingProject.view;

import java.awt.Container;
import java.util.Calendar;
import java.util.Collection;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import org.colswe.testingProject.control.ApplicationControl;
import org.colswe.testingProject.control.ApplicationTypeControl;
import org.colswe.testingProject.entity.Application;
import org.colswe.testingProject.entity.ApplicationType;
import org.colswe.testingProject.entity.Student;

/**
 *
 * @author andres
 */
public class CreateApplication extends javax.swing.JPanel {

    private Student student;

    /**
     * Creates new form CreateApplication
     */
    public CreateApplication(Student student) {
        this.student = student;
        initComponents();
    }

    private ComboBoxModel generateModel() {
        Collection<ApplicationType> types = new ApplicationTypeControl().readAll();
        String[] data;
        data = new String[types.size()];
        int i = 0;
        for (ApplicationType ap : types) {
            data[i] = ap.getName();
            i++;
        }
        return new DefaultComboBoxModel(data);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCreate = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDocuments = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        txtPeriod1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPeriod2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cmbType = new javax.swing.JComboBox();

        setMaximumSize(new java.awt.Dimension(730, 650));
        setMinimumSize(new java.awt.Dimension(730, 650));

        btnCreate.setText("Crear Solicitud");
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancelar");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jLabel1.setText("Documentos");

        txtDocuments.setColumns(20);
        txtDocuments.setRows(5);
        jScrollPane1.setViewportView(txtDocuments);

        jLabel2.setText("Periodo");

        txtPeriod1.setText("AAAA");
        txtPeriod1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPeriod1FocusGained(evt);
            }
        });

        jLabel3.setText("-");

        txtPeriod2.setText("S");

        jLabel4.setText("Tipo");

        cmbType.setModel(generateModel());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtPeriod1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPeriod2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btnCreate)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnCancel))
                                .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(211, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(84, 84, 84)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPeriod1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtPeriod2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCreate)
                    .addComponent(btnCancel))
                .addContainerGap(344, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtPeriod1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPeriod1FocusGained
        txtPeriod1.setText("");
        txtPeriod2.setText("");
    }//GEN-LAST:event_txtPeriod1FocusGained

    private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateActionPerformed
        String documents, period1, period2;
        documents = txtDocuments.getText();
        Application ap;
        ApplicationType apt = null;
        period1 = txtPeriod1.getText();
        period2 = txtPeriod2.getText();
        try {
            if (documents.length() == 0 || documents.length() > 200) {
                throw new Exception("El campo documentos es obligatorio y no puede"
                        + " contener más de 200 caracteres");
            }
            if (period1.length() != 4) {
                throw new Exception("El periodo debe ser del formato AAAA-S");
            }
            if (period2.length() != 1) {
                throw new Exception("El periodo debe ser del formato AAAA-S");
            }
            for (ApplicationType aptt : new ApplicationTypeControl().readAll()) {
                if (aptt.getName().equals(cmbType.getSelectedItem())) {
                    apt = aptt;
                }
            }
            ap = new Application("En coordinación curricular", "", documents, period1 + "-" + period2, Calendar.getInstance().getTime(), apt, null);
            student.add(ap);
            ap.setStudent(student);
            if (new ApplicationControl().create(ap)) {
                JOptionPane.showMessageDialog(this, "Creación Correcta", "Información", JOptionPane.INFORMATION_MESSAGE);
                Container c = getParent();
                c.setVisible(false);
                c.removeAll();
                String id = student.getIdNumber();
                student = new Student();
                student.setIdNumber(id);
                c.add(new ApplicationPanel(student));
                c.setVisible(true);
            } else {
                JOptionPane.showMessageDialog(this, "No se pudo crear", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnCreateActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        Container c = getParent();
        c.setVisible(false);
        c.removeAll();
        c.add(new ApplicationPanel());
        c.setVisible(true);
    }//GEN-LAST:event_btnCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreate;
    private javax.swing.JComboBox cmbType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtDocuments;
    private javax.swing.JTextField txtPeriod1;
    private javax.swing.JTextField txtPeriod2;
    // End of variables declaration//GEN-END:variables
}
